/**
 * Created by win7 on 2018/2/22.
 * change by wonderful 20180812 在原来的基础上进行了较深的封装，调用更简单，更灵活。
 *{
 * gridOption://grid控件参数
 * rowData://需要显示详细的行数据
 * workFlowId://流程节点id
 * tabId://详细的tab面板id
 * bStatusOpts:业务状态参数 格式如：[{text:"新建计划",value:1}]
 * tStatusOpts:任务状态参数 格式如：[{text:"待处理",value:0}]
 * actionControl:function(tabObj,rowData,bStatusOpts,tStatusOpts){//输入框以及操作按钮控制
 *         var btns=[];
 *         if(rowData.bStauts==1){
 *         var passBtn=$("<input type="button" id="tab_pass_btn" class="input_button" value="审核通过">);
 *         var unPassBtn=$("<input type="button" id="tab_unpass_btn" class="input_button" value="审核不通过">");
 *         btns.push(passBtn);
 *         btns.push(passBtn);
 *         tabObj.find("textarea[name='remark']").show();
 *         }
 *         return btns;
 * }
 * }
 *
 *
 */


function ifNull(obj){
    if(obj==0){
        return obj;
    }else if(new String(obj).toUpperCase()=="NULL"){
        return "";
    }
    return obj?obj:"";
}

function showDetailFun(opt) {
    var detailTabOpt = {
        width :width,// 宽度
        height :height,// 高度
        isAutoWidth :false,// 是否自适应宽度，优先级高于width。
        isAutoHeight :false,// 是否自适应高度，优先级高于height。
        event:{},
        tabs : [// 选项卡定义
            {
                title : "",// 选项卡标题
                onclick : function(tab, content, tabObj) {// 选项卡单击事件，参数分别为选项卡对象、内容对象、整个选项卡控件对象。

                }
            } ]
    };
    var rowData=opt.rowData;
    var detailTab=$("#"+opt.tabId).wdfTab("init",detailTabOpt);
    var tStatusOpts=opt.tStatusOpts;
    var bStatusOpts=opt.bStatusOpts;
    var gridOption=opt.gridOption

    detailTab.data("curRowData",rowData);

    if(detailTab){
        for(var key in rowData){
            var fields= detailTab.find(".tab_left_top .tab_top_field");
            var fieldValue=fields.find("div[id^="+key+"]");
            fieldValue.html(ifNull(rowData[key]));
            if(fieldValue.hasClass("statusFormatColor")){
                fieldValue.css("color",getFormatColor(rowData[key]));
            }
        }
        //查看图片测试
        $(".chakan").colorbox({rel:'.chakan',width:'60%',height:"600px"});

        detailTab.find(".tabBar").find(".show").click();
        workProgressRightListFun(rowData.bId,opt.workFlowId,detailTab,bStatusOpts,tStatusOpts);
        getWorkProgressFun(rowData.bId,opt.workFlowId,detailTab,tStatusOpts);

        if (opt.actionControl) {
            var actionBtns = opt.actionControl(detailTab, rowData, bStatusOpts,tStatusOpts);
            if (actionBtns) {
                detailTab.find(".tab_left_form_opt").empty();
                for (var i = 0; i < actionBtns.length; i++) {
                    var actionBtn = actionBtns[i];
                    detailTab.find(".tab_left_form_opt").append(actionBtn);
                }
            }
        }

        if(gridOption!=null&&typeof(gridOption)!="array"){
            gridOption=[gridOption];
        }else if(!gridOption){
            detailTab.show();
            return detailTab;
        }
        for(var i=0;i<gridOption.length;i++){
            var gridOpt=gridOption[i];
            $("#"+gridOpt.id).empty();
            setTimeout(function(){$("#"+gridOpt.id).wdfGrid("init",gridOpt);},1) ;
        }
        detailTab.show();
        // resizeContent(detailTab);
        return detailTab;
    }
}

//更加窗体尺寸大小变化动态改变内容的尺寸大小
function resizeContent(detailTab){
   // var tabBar=detailTab.find(".tabBar");
   // var contentbar=detailTab.find(".contentBar");
   // var content=contentbar.find(".content");
   // var tab_left=detailTab.find(".tab_left");
   // var tab_right=detailTab.find(".tab_right");
   // var height=$(window).height()-tabBar.height()-40;
   // var rightProgress=tab_right.find(".progress_box");
   // var progressTitle=rightProgress.find(".progress_tit");
   // var progressContent=rightProgress.find(".progress_content");
   detailTab.css("overflow","hidden");

}

function getWorkProgressFun(bId,workFlowId,detailTab,tStatusOpts){
    $.ajax({
        url:basePath+"/workFlow/listWorkFlowLogs.wdf",
        //processData: false,
        //data: xmlDocument,
        async: true,
        dataType: "json",
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
        // data: encodeURI(option.param),//解决页面默认为GBK编码导致的Ajax传输中文乱码问题
        data:{workFlowId: workFlowId,bId: bId},
        beforeSend: null,
        success: function (data) {
            var  flowWorkData = data.data;

            var content  = "";
            if(detailTab){
                detailTab.find("#workFlowLogs_box").html("");
            }
            for(var i=0;i<flowWorkData.length;i++) {
                var documentFileUrlArr = new Array();
                if(flowWorkData[i].documentFileUrl){
                    documentFileUrlArr = flowWorkData[i].documentFileUrl.split(",");
                }
                var creatTime = formatDateTime(flowWorkData[i].createTime);
                var stateColor;
                if(flowWorkData[i].pCode == TSTATUS_FAIL){
                    stateColor = "unpassState";
                }else{
                    stateColor ="passState";
                }
                var dlObj= $('<dl>' +
                    // '<dt><img src="'+userImg+'/'+flowWorkData[i].headerpath+'" onerror="javascript:this.src=\''+basePath+'resource/main/images/touxiang.jpg'+'\'" /></dt>' +
                    '<dd class="workFlowLogs_con">'+
                    '<div class="workFlowLogs_line1">' +
                    '<span class="operation_name">'+ifNull(flowWorkData[i].processerName)+'</span>' +
                    '<span class="operation_date">'+creatTime+'</span></div>'+
                    '<span class="operation_state '+stateColor+'">'+ifNull(getOptionText(tStatusOpts,flowWorkData[i].pCode))+'</span>' +
                    '<span class="operation_text">'+ifNull(flowWorkData[i].detail)+'</span></dd><dd class="clear"></dd>');
                if(detailTab){
                    dlObj.appendTo(detailTab.find("#workFlowLogs_box"));
                   if(flowWorkData[i].fileNames){
                        var imgArrs = flowWorkData[i].fileNames.split(",");
                        var imgContent = "";
                       imgContent += "<dd class='workFlow_img_box'>";
                        for (var j = 0;j < imgArrs.length; j++){
                            imgContent += '<a class="workFlow_img" href="'+basePath+'/filesManager/showFile?url='+imgArrs[j]+'"><img src="'+basePath+'/filesManager/showFile?url='+imgArrs[j]+'"/></a>';
                        }
                       imgContent += "<div class='clear'></div></dd>";
                       dlObj.append($(imgContent));
                       $(".workFlow_img").colorbox({rel:'.chakan',width:'60%',height:"600px"});
                   }
                }

                if(flowWorkData[i].documentFileUrl&&flowWorkData[i].documentFileUrl!=""){
                    var downLoadDocFilesBtn=$("<a  class='enclosure_btn' id='enclosureBtn_ProgressLogsList_DpaosDetailTab_"+i+"' " +
                        " href=\"javascript:\"" +
                        " >查看附件</a>");
                    var docFiliesUrl=flowWorkData[i].documentFileUrl;

                    function getShowFilesListEvent(flowWorkData,filesBtn){
                        var status=flowWorkData.status;
                        return function(e){
                            stopPropagation(e);
                            littileTools.filesListPanel.show(filesBtn.data("docFiliesUrl"),filesBtn,getDocDirPath(status));
                        };
                    }
                    downLoadDocFilesBtn.data("docFiliesUrl",docFiliesUrl);
                    downLoadDocFilesBtn.bind("click",getShowFilesListEvent(flowWorkData[i],downLoadDocFilesBtn));
                    dlObj.append(downLoadDocFilesBtn);
                }


            }

        }
    });
}

function workProgressRightListFun(bId,workFlowId,detailTab,bStatusOpts,tStatusOpts) {
    var url = basePath+"/workFlow/listWorkProgress.wdf";
    if(workFlowId==WORKFLOW_INSPECTION){ //判断工作流程节点是否为养护计划巡检中的节点，如果是则不走默认的工作流路劲。yuanp 20190930
        url= basePath+"/inspectionAndMaint/listWorkProgress"
    }
    $.ajax({
        url:url,
        //processData: false,
        //data: xmlDocument,
        async: true,
        dataType: "json",
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
        // data: encodeURI(option.param),//解决页面默认为GBK编码导致的Ajax传输中文乱码问题
        // 1 workFlowID:'zxgc_WorkFlow' 项目流程 ，
        // 2 workFlowID:'zxgc_DeviceProcPlan_WorkFlow' 计划流程
        // 3 workFlowID:'zxgc_DeviceOutStorgeAndProc_WorkFlow' 采购出库清单流程
        // 4 workFlowID:'zxgc_DeviceCancelingStocks_WorkFlow' 采购出库清单流程
        data:{workFlowId: workFlowId,bId:bId},
        beforeSend: null,
        success: function (data) {
            var workProgressData = data.data;
            $("#progress_content").html("");
            var content = "";
            var flowNodeID;
            for( var i=0;i<workProgressData.length;i++){
                var flowNodeIDStat = "";
                if(i==0){
                    flowNodeID = workProgressData[i].flowNodeID;
                    flowNodeIDStat = "stating"
                }
                // if(flowNodeID == workProgressData[i].flowNodeID){
                //     flowNodeIDStat = "stating"
                // }else{
                //     flowNodeIDStat = ""
                // }

                var stateColor;
                if(workProgressData[i].tStatus == TSTATUS_FAIL
                ){
                    stateColor = "unpassState";
                }else if(workProgressData[i].tStatus == TSTATUS_PROCESSING||
                    workProgressData[i].tStatus == TSTATUS_SUCCESS){
                    stateColor ="passStating";
                }else if(workProgressData[i].tStatus==TSTATUS_PEINDING){
                    stateColor ="peingindState";
                }else{
                    stateColor ="passState";
                }

                var reviceTime = formatDateTime(workProgressData[i].reviceTime);
                content += '<dl class="clear">';
                content += '<dt><span class="flowNodeIDStat '+flowNodeIDStat+'"></span></dt>';
                content += '<dd>' +
                    '<span class="progress_data">'+reviceTime+'</span>' +
                    '<div><span class="progress_user">'+ifNull(workProgressData[i].processerName)+'</span>' +
                    '<span class="progress_name">'+ifNull(workProgressData[i].flowNodeName)+'</span></div>' +
                    '<span class="progress_state '+stateColor+'">'+ifNull(getOptionText(tStatusOpts,workProgressData[i].tStatus))+'</span>'+
                    '</dd><dd style="clear: both;float: none"></dd>';

                content += '</dl>';
            }
            $("#progress_content").append(content);
            // if(detailTab){
            //     detailTab.find("#progress_content").append(content);
            // }else {
            //
            // }

        }
    });
}

function getFormatColor(bStatusName) {
    var returnColor = "";
    if(bStatusName == "初稿" || bStatusName.indexOf("失败") != -1 || bStatusName.indexOf("未") != -1){
        returnColor = "#fc4528";
    }else if(bStatusName == "生效" || bStatusName.indexOf("通过") != -1){
        returnColor = "#22d655";
    }else if(bStatusName.indexOf("中") != -1){
        returnColor = "#fd7013";
    }else{
        returnColor = "#737a85";
    }
    return returnColor;

}
